<template>
  <div class="chongzhi-box">
    <div class="head-box w-card">
      <div class="title-box">
        <span>{{ shopInfo.shopName || '' }}</span>
      </div>
      <div class="jine-box w-flex">
        <span>冻结佣金(元)：</span>
        <span class="jine-text">{{ numFormat(shopInfo.incomeFreezen/10000) || '' }}</span>
      </div>
    </div>
    <div class="table-box w-card">
      <div class="title-box">
        <span>冻结明细</span>
      </div>
      <div class="search-box w-flex w-f-a-c w-mt-20 w-gap-10">
        <div class="w-search-item">
          <span>关联订单：</span>
          <el-input v-model="searchForm.businessOrderId" clearable placeholder="请输入关联订单" />
        </div>
        <div class="w-search-item">
          <span>生成时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" />
        </div>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-content">
        <el-table :max-height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe @sort-change="sortChange">
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="incomeOrderId" label="佣金ID" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeOrderId || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="goodsName" label="商品名称" align="center">
            <template v-slot="scope">
              {{ scope.row.goodsName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="income" label="冻结金额(元)" align="center">
            <template v-slot="scope">
              {{ numFormat(scope.row.income) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="businessOrderId" label="关联订单" align="center">
            <template v-slot="scope">
              <el-link type="primary"
                @click="toOrderDetail(scope.row.businessOrderId)">{{ scope.row.businessOrderId || '' }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="冻结时间" align="center">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from 'vue'
import { warningDeal, numFormat, getWindowWidthAndHeight, getDateTime } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import api from '../api'
export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      shopInfo: {},
      searchForm: {},
      tableData: [],
    })

    function getShopInfo(shopId) {
      const obj = {
        shopId
      }
      api.shopDetail(obj).then(res => {
        if (res.code == '200') {
          state.shopInfo = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        orderBy: 'id desc',
        incomeState: 1,
        shopId: route.query.shopId,
        ...state.searchForm,
        startTime: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[0].getTime() : null,
        endTime: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[1].getTime() : null,
      }
      api.capitalIncomeFlow(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function toOrderDetail(orderId) {
      //去订单详情
      router.push({ path: '/fxOrder/detail', query: { sysOrderId: orderId } })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 250
      const shopId = route.query.shopId
      if (shopId) {
        getShopInfo(shopId)
      }
      getTableData()
    })

    return {
      ...toRefs(state),
      numFormat,
      searchHandle,
      resetHandle,
      handleCurrentChange,
      handleSizeChange,
      toOrderDetail,
      getDateTime,
    }
  }
}
</script>

<style lang="scss" scoped>
.chongzhi-box {
  width: 100%;
  .head-box {
    width: 100%;
    box-sizing: border-box;
    padding: 20px;
  }
  .table-box {
    width: 100%;
    box-sizing: border-box;
    padding: 20px;
    margin-top: 20px;
  }
}
.title-box {
  font-size: 16px;
  font-weight: bold;
}
.jine-box {
  display: flex;
  justify-content: flex-end;
  font-size: 14px;
  color: #727780;
  align-items: flex-end;
}
.jine-text {
  font-size: 24px;
  color: #353a43;
}
.table-content {
  margin-top: 10px;
  .table-footer {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>